package com.lmc.edrive.login;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;



import com.lmc.edrive.R;
import com.lmc.edrive.base.EDriveBaseActivity;
import com.lmc.edrive.comon.HttpASync;
import com.lmc.edrive.comon.HttpRequest;
import com.lmc.edrive.comon.HttpASync.OnRequest;
import com.lmc.edrive.constant.AppConstant;
import com.lmc.edrive.home.HomeScreenActivity;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.Preference;

public class Login extends EDriveBaseActivity implements OnClickListener, OnRequest {
	private EditText userName;
	private EditText password;
	private ImageButton login;
	private ImageButton cancel;
	private Context context;
	private TextView newUser;
	private String uName;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		context=this;
		userName=(EditText) findViewById(R.id.vehicle_name);
		password=(EditText) findViewById(R.id.editText2);
		login=(ImageButton)findViewById(R.id.imageButton1);
		cancel=(ImageButton)findViewById(R.id.imageButton2);
		newUser=(TextView)findViewById(R.id.textView5);
		login.setOnClickListener(this);
		cancel.setOnClickListener(this);
		newUser.setOnClickListener(this);
		
		
	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public void onClick(View v) {
		int id=v.getId();
		if(id==R.id.imageButton1) {
			uName=userName.getEditableText().toString();
			String pwd=password.getEditableText().toString();
			System.out.println("username="+uName);
			System.out.println("password="+pwd);
			if(uName.equals("")&& (pwd.equals(""))) {
				Toast.makeText(context, "Please enter username and pasword", Toast.LENGTH_SHORT).show();
			}
			else if(uName.equals("")) {
				Toast.makeText(context, "Please enter username", Toast.LENGTH_SHORT).show();
			}
			else if(pwd.equals("")) {
				Toast.makeText(context, "Please enter password", Toast.LENGTH_SHORT).show();
			}else {
			checkAuthentication(uName, pwd);
			//System.out.println("login="+login);
			
			}
				
		}
		else if(id==R.id.imageButton2) {
			onBackPressed();
		}
		else if(id==R.id.textView5) {
			//Toast.makeText(context, "Register", Toast.LENGTH_SHORT).show();
			Intent i=new Intent(context, RegisterActivity.class);
			startActivity(i);
			//finish();
		}
	}

	private boolean checkAuthentication(String userName,String password) {
		System.out.println("username="+userName);
		System.out.println("password="+password);
		String loginPath=AppConstant.LOGIN_PATH+"?username="+userName+"&password="+password;
		HttpASync async = new HttpASync();
		
		async.setOnRequestListener(this);
		async.execute(loginPath);
		showProgress("Authenticating... Please Wait");
		/*System.out.println("loginpath="+loginPath);
		String respStr=HttpRequest.getResponse(loginPath);
			if(respStr.contains("true")) {
				return true;
			}*/
			
	
		
		
		
		return false;	
	}
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		finish();
	}

	@Override
	public void onResponseReceived(String response) {
		boolean login = false;
		if(response!=null&&response.contains("true")) {
			login = true;
			
		}
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				hideProgress();
				
			}
		});
		
		if(login) {
			runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					Toast.makeText(context, "login successfull", Toast.LENGTH_SHORT).show();
					Intent i=new Intent(context, HomeScreenActivity.class);
					SharedPreferences pref = getSharedPreferences(AppConstant.PREFS_NAME,0);
					SharedPreferences.Editor editor = pref.edit();
					editor.putString("username", uName);
					editor.commit();
					startActivity(i);
					finish();
				}
			});
			
			
		}else {
			runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					Toast.makeText(context, "login unsuccessfull", Toast.LENGTH_SHORT).show();
					
				}
			});
			
		}
		
	}
	

}
